package com.yanqu.road.server.manger.config;

import com.yanqu.road.entity.config.system.SystemOpenInfo;
import com.yanqu.road.entity.enums.ePlayerAttrType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.estatewar.EstateBuildingInfo;
import com.yanqu.road.entity.estatewar.UserEstateWarData;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.entity.task.UserTaskData;
import com.yanqu.road.logic.bussiness.config.SystemOpenBussiness;
import com.yanqu.road.logic.config.ChannelConfig;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.passRoad.PassRoadModule;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.gameplayer.module.player.AttributeModule;
import com.yanqu.road.server.gameplayer.module.player.BuildingModule;
import com.yanqu.road.server.gameplayer.module.player.EarnSpeedModule;
import com.yanqu.road.server.gameplayer.module.player.EstateWarModule;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.manger.ConfigMgr;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.config.grandchild.GrandChildConfigMgr;
import com.yanqu.road.server.manger.manor.FishPondMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.utils.date.DateHelper;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 系统解锁
 */
public class SystemOpenMgr extends TempMgr {

	private static Map<Integer, SystemOpenInfo> systemOpenInfoMap;

	@Override
	public boolean reloadConfig() {
		systemOpenInfoMap = SystemOpenBussiness.getSystemOpenInfoMap();
		return true;
	}

	@Override
	public boolean reloadData() throws Exception {
		return true;
	}

	@Override
	public boolean init() throws Exception {
		return reload();
	}

	@Override
	public boolean stop() {
		return false;
	}

	@Override
	public boolean save() {
		return false;
	}

	public static SystemOpenInfo getSystemOpenInfo(int systemId) {
		return systemOpenInfoMap.get(systemId);
	}

	/**
	 * 判断系统是否达到解锁条件
	 */
	public static boolean systemOpen(GamePlayer player, int systemId) {
		SystemOpenInfo systemOpenInfo = getSystemOpenInfo(systemId);
		if (null == systemOpenInfo) {
			return false;
		}
		if (offlineSystemOpen(player.getUserInfo().getSystemOpenValue(), systemId) && systemId != eSystemId.FishPond.getValue()) {//已经解锁
			if (systemId == eSystemId.MusicRoom.getValue()) {//妙音坊
				if (systemOpenInfo.getConditionType() == 2) {//身份
					if (player.getTitleId() < systemOpenInfo.getValue().intValue()) {
						return false;
					}
				}
				int days = GameConfig.MUSIC_ROOM_UNLOCK_COND_OPEN_SERVICE_DAY;
				if (!ServerListMgr.checkSystemOpenByServerOpenDay(days, GameServer.getInstance().getServerId()) ||
						player.getModule(BuildingModule.class).getBuildingById(GameConfig.MUSIC_ROOM_OPEN_BUILDING_ID) == null) {
					return false;
				}
			}
			return true;
		}
		if (systemOpenInfo.getConditionType() == 1) {//赚速
			return player.getModule(EarnSpeedModule.class).getEarnSpeed().compareTo(systemOpenInfo.getValue()) >= 0;
		} else if (systemOpenInfo.getConditionType() == 2) {//身份
			//身份+客栈已建造
			if (systemOpenInfo.getSystemId() == eSystemId.Inn.getValue()) {
				if (player.getModule(BuildingModule.class).getBuildingById(GameConfig.INN_BUILD_ID) == null) {
					return false;
				}
			}else if (systemOpenInfo.getSystemId() == eSystemId.Clinic.getValue()) {//医馆建造
				if(player.getModule(BuildingModule.class).getBuildingById(GameConfig.CLINIC_BUILD_ID) == null) {
					return false;
				}
				if(ConfigMgr.isZhenHuan20()){
					int openServerDay = ServerListMgr.getServerOpenDay(GameServer.getInstance().getServerId());
					if(openServerDay < GameConfig.HH_CLINIC_UNLOCK_DAY){
						return false;
					}
				}
			}else if (systemOpenInfo.getSystemId() == eSystemId.HouseBank.getValue()) {//钱庄系统
				if(player.getUserInfo().getHouseLv() < GameConfig.HOUSE_BANK_UNLOCK_HOUSE_LEVEL) {
					return false;
				}
				if(ConfigMgr.isZhenHuan20()){
					int openServerDay = ServerListMgr.getServerOpenDay(GameServer.getInstance().getServerId());
					if(openServerDay < GameConfig.HH_HOUSE_BANK_UNLOCK_DAY){
						return false;
					}
				}
			}else if (systemOpenInfo.getSystemId() == eSystemId.ThiefSystem.getValue()) {// 小偷解锁
				if(player.getModule(BuildingModule.class).getBuildingById(GameConfig.THIEF_UNLOCK_BUILDING_ID) == null) {
					return false;
				}
			}else if (systemOpenInfo.getSystemId() == eSystemId.SmallGame.getValue()) {
				if(ConfigMgr.isZhenHuan20()){
					int openServerDay = ServerListMgr.getServerOpenDay(GameServer.getInstance().getServerId());
					if(openServerDay < GameConfig.HH_SMALLGAME_UNLOCK_DAY){
						return false;
					}
				}
			}else if (systemOpenInfo.getSystemId() == eSystemId.EarnSpeedFundPlusActivity.getValue()) {//精进基金
				if(!NormalActivityMgr.isEarnMoneyFundMinGoalCompete(player) || player.getModule(AttributeModule.class).getAttribute(ePlayerAttrType.EarnSpeedFund) != 1) {
					return false;
				}
			}else if (systemOpenInfo.getSystemId() == eSystemId.WineShop.getValue()) {//酒肆建造
				if (player.getModule(BuildingModule.class).getBuildingById(GameConfig.WINESHOP_BUILDING_ID) == null) {
					return false;
				}
				int days = GameConfig.WINESHOP_OPEN_SERVICE_TIME_LIMIT;
				if (!ServerListMgr.checkSystemOpenByServerOpenDay(days, GameServer.getInstance().getServerId())) {
					return false;
				}
			}else if (systemOpenInfo.getSystemId() == eSystemId.DrugStore.getValue()){//药铺
				if (player.getModule(BuildingModule.class).getBuildingById(GameConfig.DRUGSTORE_BUILDING_ID) == null) {
					return false;
				}
				if(ConfigMgr.isZhenHuan20()){
					int openServerDay = ServerListMgr.getServerOpenDay(GameServer.getInstance().getServerId());
					if(openServerDay < GameConfig.HH_DRUGSTORE_UNLOCK_DAY){
						return false;
					}
				}
			}else if (systemOpenInfo.getSystemId() == eSystemId.BeautyHigherFund.getValue()) {//倾城基金
				if(!NormalActivityMgr.isBeautyHigherFundCanBuy(player) || player.getModule(AttributeModule.class).getAttribute(ePlayerAttrType.BeautyFund) != 1) {
					return false;
				}
			}else if (systemOpenInfo.getSystemId() == eSystemId.FishPond.getValue()) {//鱼塘
				if (!ServerListMgr.checkSystemOpenByServerOpenDay(GameConfig.FISHPOND_OPEN_SERVICE_TIME_LIMIT, GameServer.getInstance().getServerId())) {
					return false;
				}
				if (!FishPondMgr.checkFishPondOpen(player)) {
					return false;
				}
			}else if (systemOpenInfo.getSystemId() == eSystemId.GrandChild.getValue()) {//孙子
				//开服天数 豪绅大院  区服系统开放
				UserEstateWarData userEstateWarData = player.getModule(EstateWarModule.class).getUserEstateWarData();
				if (userEstateWarData == null) {
					return false;
				}
				if (!GrandChildConfigMgr.isGrandChildOpen() ||  userEstateWarData.getType() < GameConfig.GRAND_CHILD_UNLOCK_HOUSE_TYPE) {
					return false;
				}
			}else if (systemOpenInfo.getSystemId() == eSystemId.WingRoomUnlockTask.getValue()) {//厢房家具目标
				if (!player.getModule(EstateWarModule.class).isCanOpenFurnitureTargetTask()) {
					return false;
				}
			}else if (systemOpenInfo.getSystemId() == eSystemId.WingRoom.getValue()) {//厢房
				if (!player.getModule(TaskModule.class).wingRoomUnlockTaskAllFinish()) {
					return false;
				}
			}else if (systemOpenInfo.getSystemId() == eSystemId.Winery.getValue()) {//酒坊
				if (player.getModule(BuildingModule.class).getBuildingById(GameConfig.WINERY_OPEN_BUILD_ID) == null) {
					return false;
				}
				if(ConfigMgr.isZhenHuan20()){
					int openServerDay = ServerListMgr.getServerOpenDay(GameServer.getInstance().getServerId());
					if(openServerDay < GameConfig.HH_WINERY_UNLCOK_DAY){
						return false;
					}
				}
			}else if (systemOpenInfo.getSystemId() == eSystemId.WasheActivity.getValue()) {// 瓦舍夺魁
				ServerInfo serverInfo = ServerListMgr.getServerInfo(GameServer.getInstance().getServerId());
				if (serverInfo == null || System.currentTimeMillis() - serverInfo.getOpenTime() * 1000 < GameConfig.WASHEPVP_OPEN_DAY * DateHelper.DAY_MILLIONS) {
					return false;
				}
			}else if (systemOpenInfo.getSystemId() == eSystemId.Cricket.getValue()) {// 蛐蛐系统
				int days = GameConfig.CRICKET_OPEN_DAY;
				if (!ServerListMgr.checkSystemOpenByServerOpenDay(days, GameServer.getInstance().getServerId())) {
					return false;
				}
			}else if (systemOpenInfo.getSystemId() == eSystemId.CricketFund.getValue()) {//促织基金
				int days = GameConfig.CRICKET_OPEN_DAY;
				if (!ServerListMgr.checkSystemOpenByServerOpenDay(days, GameServer.getInstance().getServerId())) {
					return false;
				}
			}else if (systemOpenInfo.getSystemId() == eSystemId.UnionCampWar.getValue()) {// 商帮
				int days = GameConfig.SHANGBANG_OPEN_COND_SERVICE_DAY;
				if (!ServerListMgr.checkSystemOpenByServerOpenDay(days, GameServer.getInstance().getServerId())) {
					return false;
				}
			} else if (systemOpenInfo.getSystemId() == eSystemId.Kuang.getValue()) {	// 矿场争夺战
				int days = GameConfig.KUANG_OPEN_DAY;
				if (!ServerListMgr.checkSystemOpenByServerOpenDay(days, GameServer.getInstance().getServerId())) {
					return false;
				}
			}else if (systemOpenInfo.getSystemId() == eSystemId.VehicleSoul.getValue()) {//魂石还要开服天数
				int days = GameConfig.VEHICLES_SOUL_UNLOCK_LIMIT;
				if (!ServerListMgr.checkSystemOpenByServerOpenDay(days, GameServer.getInstance().getServerId())) {
					return false;
				}
			}else if (systemOpenInfo.getSystemId() == eSystemId.NewMonopolyMarket.getValue()){
				int days = GameConfig.NEWMONOPOLYMARKET_UNLOCK_DAY;
				if (!ServerListMgr.checkSystemOpenByServerOpenDay(days, GameServer.getInstance().getServerId())) {
					return false;
				}
			}else if (systemOpenInfo.getSystemId() == eSystemId.Curio.getValue()){
				int days = GameConfig.CURIO_OPEN_COND_SEVER_DAY;
				if (!ServerListMgr.checkSystemOpenByServerOpenDay(days, GameServer.getInstance().getServerId())) {
					return false;
				}
				//府邸
				UserEstateWarData userEstateWarData = player.getModule(EstateWarModule.class).getUserEstateWarData();
				if(userEstateWarData == null){
					return false;
				}
				EstateBuildingInfo buildingInfo = EstateWarConfigMgr.getEstateBuildingInfo(userEstateWarData.getType());
				if(buildingInfo == null){
					return false;
				}
				if(buildingInfo.getId() < GameConfig.CURIO_OPEN_COND_ESTATE_HOUSE){
					return false;
				}
			} else if (systemOpenInfo.getSystemId() == eSystemId.MusicRoom.getValue()) {//妙音坊
				int days = GameConfig.MUSIC_ROOM_UNLOCK_COND_OPEN_SERVICE_DAY;
				if (!ServerListMgr.checkSystemOpenByServerOpenDay(days, GameServer.getInstance().getServerId())) {
					return false;
				}
				if (player.getModule(BuildingModule.class).getBuildingById(GameConfig.MUSIC_ROOM_OPEN_BUILDING_ID) == null) {
					return false;
				}
			} else if (systemOpenInfo.getSystemId() == eSystemId.Kowloon.getValue()) { // 九龙城寨
				ServerInfo serverInfo = ServerListMgr.getServerInfo(GameServer.getInstance().getServerId());
				if (serverInfo != null && serverInfo.getOpenTime() >= ChannelConfig.KOWLOON_VERSION_UPDATE_TIMESTAMP) {
					// 开服在xxx之后的才做开服天数判断
					int days = GameConfig.KOWLOON_OPEN_SERVICE_TIME_LIMIT;
					if (!ServerListMgr.checkSystemOpenByServerOpenDay(days, GameServer.getInstance().getServerId())) {
						return false;
					}
				}
			}else if (systemOpenInfo.getSystemId() == eSystemId.VehicleBone.getValue()) {//魂石还要开服天数
				int days = GameConfig.SOUL_BONE_UNLOCK_LIMIT;
				if (!ServerListMgr.checkSystemOpenByServerOpenDay(days, GameServer.getInstance().getServerId())) {
					return false;
				}
			} else if (systemOpenInfo.getSystemId() == eSystemId.TravelBook.getValue()) {
				int days = GameConfig.TRAVEL_BOOK_TIME_LIMIT;
				if (!ServerListMgr.checkSystemOpenByServerOpenDay(days, GameServer.getInstance().getServerId())) {
					return false;
				}
			}
			return player.getTitleId() >= systemOpenInfo.getValue().intValue();
		} else if (systemOpenInfo.getConditionType() == 3) {//主线任务
			int mainTaskId = player.getModule(TaskModule.class).getRealMainTaskId();
			if (mainTaskId == systemOpenInfo.getValue().intValue()) {
				UserTaskData userTaskData = player.getModule(TaskModule.class).getUserTaskData(mainTaskId);
				if (null != userTaskData && userTaskData.getState() == 1) {
					return true;
				}
			} else {
				return mainTaskId > systemOpenInfo.getValue().intValue();
			}
		} else if (systemOpenInfo.getConditionType() == 5) {//主线关卡
			if (0 == systemOpenInfo.getValue().intValue()) {
				return true;
			}
			int passId = player.getModule(PassRoadModule.class).getUserPassId();
			return passId > systemOpenInfo.getValue().intValue();
		} else if (systemOpenInfo.getConditionType() == 6) {//门客数量大于N
			if (0 == systemOpenInfo.getValue().intValue()) {
				return true;
			}
			//厢房命盘系统，前提是:厢房系统解锁
			if (systemOpenInfo.getSystemId() == eSystemId.Chakra.getValue()) {
				if (!SystemOpenMgr.systemOpen(player, eSystemId.WingRoom.getValue())) {
					return false;
				}
			}
			//获取门客数量
			int patronsNum = player.getModule(PatronsModule.class).getUserPatronsMap().size();
			return patronsNum >= systemOpenInfo.getValue().intValue();
		}
		return false;
	}

	public static List<SystemOpenInfo> getSystemOpenInfoList(){
		return new ArrayList<>(systemOpenInfoMap.values());
	}

	/**
	 * 离线玩家检测系统解锁
	 */
	public static boolean offlineSystemOpen(BigInteger systemOpenValue, int systemId){
		if(null == systemOpenValue){
			return false;
		}
		BigInteger systemValue = BigInteger.valueOf(2).pow(systemId);
		return systemValue.compareTo(systemOpenValue.and(systemValue)) == 0;
	}

	/**
	 * 检查系统解锁
	 * @param userId
	 * @param systemId
	 * @return
	 */
	public static boolean checkSystemOpen(long userId, int systemId) {
		GamePlayer gamePlayer = GamePlayerMgr.getOnlinePlayer(userId);
		if (gamePlayer != null) {
			return systemOpen(gamePlayer, systemId);
		} else {
			UserInfo userInfo = UserMgr.getUserInfo(userId);
			if (userInfo != null) {
				return offlineSystemOpen(userInfo.getSystemOpenValue(), systemId);
			}
		}
		return false;
	}
}
